package nl.weeaboo.sh;

import java.awt.Font;
import java.io.IOException;

import javax.media.opengl.GL;
import javax.swing.SwingUtilities;

import nl.weeaboo.game.Log;
import nl.weeaboo.game.ResourceManager;
import nl.weeaboo.game.debug.DebugFrame;
import nl.weeaboo.game.gl.GLManager;
import nl.weeaboo.game.gl.ScreenCapture;
import nl.weeaboo.game.input.UserInput;
import nl.weeaboo.sh.level.AbstractLevel;
import nl.weeaboo.sh.view.CgManager;
import nl.weeaboo.sh.view.CharaImage;
import nl.weeaboo.sh.view.CharaImageStore;
import nl.weeaboo.sh.view.GameView;

public class Game extends nl.weeaboo.game.Game {

	public static boolean debug          = false;
	public static boolean debugMute      = debug && true;
	public static boolean debugSkipMenu  = debug && true;
	public static boolean debugCgTest    = debug && false;
	public static boolean debugDrawHitRects = debug && false;
	public static boolean debugShowFrame = debug && true;
	
	static {
		DebugFrame.show = debugShowFrame;
	}
	
	public Config config;

	private ControllerMapping controllerMapping;
	private GameFrame frame;
	private GameView view;
	private CgManager cgManager;
	private ScreenCapture screenCapture;
	private CharaImageStore charaImageStore;
	private Font defaultFont;
	private Font conversationFont;
	private boolean isStarted;
	private int frameNum;
	
	public Game(Config config) {
		this.config = config;
				
		charaImageStore = new CharaImageStore();
		cgManager = new CgManager(this);
		
		if (config.isScreenCaptureEnabled()) {
			screenCapture = new ScreenCapture(config.getScreenCaptureCRF(), 60,
					800, 480, 800, 480, true);
		}
	}
	
	//Functions
	public void dispose() {
		super.dispose();
		
		if (screenCapture != null) screenCapture.close();
		if (cgManager != null) cgManager.dispose();
	}
	
	protected GameFrame createGameFrame() {
		return frame = new GameFrame(this);
	}
	
	public void loadResources() {
		super.loadResources();		
		
		if (debugMute) {
			config.setSoundVolume(0f);
		}

		view = new GameView(this);
		
		ResourceManager rm = getResourceManager();		
		charaImageStore.load(rm);
		
		defaultFont = new Font("arial", Font.PLAIN, 20);
		conversationFont = defaultFont;
		try {
			conversationFont = Font.createFont(Font.TRUETYPE_FONT,
					rm.getInputStream("font/cambria.ttf"));
			conversationFont = conversationFont.deriveFont(20f);
		} catch (Exception e) {
			Log.warning(e);
		}
		
		controllerMapping = new ControllerMapping(config.getGamepadConfig());
		
		if (config.getSoundVolume() <= 0.001f) {
			getSoundManager().setMuted(true);
		}
		
		frameNum = 0;
		
		isStarted = true;
	}
	public void unloadResources() {
		charaImageStore.clear();
		
		super.unloadResources();
	}
	
	public void update(GL gl) {
		super.update(gl);

		if (debugCgTest) {
			cgManager.init(gl);
		}
		
		gl.setSwapInterval(config.isVSyncEnabled() ? 1 : 0);
	}

	public void update(UserInput input, float dt) {
		controllerMapping.update(input);
		
		super.update(input, dt);
				
		if (isDisposed()) {
			return;
		}
		
		AbstractLevel al = getLevel();
		if (al != null) {
			al.update(input, dt);
		}

		if ((frameNum & 31) == 0) {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					frame.setSubTitle(String.format("%02.0f FPS", frame.getCurrentFPS()));
				}
			});
		}
	}
	public void draw(GLManager glm) {
		if (isDisposed()) return;
		
		view.draw(glm, getWidth(), getHeight());
		
		if (config.isScreenCaptureEnabled()) {
			try {
				screenCapture.update(glm.getGL(), getWidth(), getHeight());
			} catch (IOException e) {
				Log.error(e);
			}
		}		
		
		super.draw(glm);
	}

	//Getters
	public GameFrame getGameFrame() { return frame; }
	public GameView getGameView() { return view; }
	public AbstractLevel getLevel() { return (AbstractLevel)super.getLevel(); }
	public CharaImageStore getCharaImageStore() { return charaImageStore; }
	public CharaImage getCharaImage(String id) {
		CharaImage image = charaImageStore.getImage(id);
		if (image == null) {
			//System.err.println(id);
		}
		return image;
	}
	public CgManager getCgManager() { return cgManager; }
	public Font getDefaultFont() { return defaultFont; }
	public boolean isStarted() { return isStarted; }
	
	//Setters
	
}
